/*
 * 英才汇硕信息科技有限公司 拥有本软件版权 2021 并保留所有权利。
 * Copyright 2021, YCHS Information&Science Techology Co.,Ltd,
 * All right reserved.
 */
package com.ychs.lawyerback.service.impl;

import com.ychs.lawyerback.common.ResultCode;
import com.ychs.lawyerback.dto.RoleOptDTO;
import com.ychs.lawyerback.dto.RoleQueryDTO;
import com.ychs.lawyerback.exception.BussinessException;
import com.ychs.lawyerback.mapper.RoleMapper;
import com.ychs.lawyerback.pojo.RoleEntity;
import com.ychs.lawyerback.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mayujing
 * @version 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Map<String, Object> getRolesByCondition(RoleQueryDTO roleQueryDTO) {
        List<RoleEntity> roles = roleMapper.getRolesByCondition(roleQueryDTO);
        int total = roleMapper.getCountByCondition(roleQueryDTO);

        Map<String, Object> result = new HashMap<>();
        result.put("list", roles);
        result.put("total", total);
        return result;
    }

    @Override
    @Transactional
    public int addRole(RoleOptDTO roleOptDTO) {

        // 校验角色名称唯一性
        if (existsByRoleName(roleOptDTO.getRoleName())) {
            throw new BussinessException(ResultCode.ROLENAME_ERROR);
        }

        // 校验权限字符唯一性
        if (existsByRoleKey(roleOptDTO.getRoleKey())) {
            throw new BussinessException(ResultCode.PERMISSION_CHARACTER);
        }

        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(roleOptDTO, roleEntity);
        // TODO: 设置创建人ID（需从登录信息获取）
        roleEntity.setCreateBy(1L);

        return roleMapper.addRole(roleEntity);
    }


    @Override
    @Transactional
    public int updateRole(RoleOptDTO roleOptDTO) {
        // 校验权限字符唯一性（排除当前角色）
        if (existsByRoleKeyExcludeCurrent(roleOptDTO.getRoleKey(), roleOptDTO.getRoleId())) {
            throw new BussinessException(ResultCode.PERMISSION_CHARACTER);
        }

        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(roleOptDTO, roleEntity);
        // 设置更新人ID（需从登录信息获取）
        roleEntity.setUpdateBy(1L);

        return roleMapper.updateRole(roleEntity);
    }

    @Override
    public boolean existsByRoleKeyExcludeCurrent(String roleKey, Long roleId) {
        return roleMapper.countByRoleKeyExcludeCurrent(roleKey, roleId) > 0;
    }

    @Override
    @Transactional
    public int deleteRole(List<Long> ids) {
        return roleMapper.deleteRole(ids);
    }

    @Override
    public boolean existsByRoleName(String roleName) {
        return roleMapper.countByRoleName(roleName) > 0;
    }

    @Override
    public boolean existsByRoleKey(String roleKey) {
        return roleMapper.countByRoleKey(roleKey) > 0;
    }
}
